#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/types.h>

#include "scull.h"
#include "log.h"

static ssize_t qset_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
    (void)sprintf(buf, "%d", scull_qset);
    return strlen(buf);
}
const struct kobj_attribute qset_attr = __ATTR_RO(qset);

static ssize_t quantum_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
    (void)sprintf(buf, "%d", scull_quantum);
    return strlen(buf);
}
const struct kobj_attribute quantum_attr = __ATTR_RO(quantum);

const struct attribute *scull_attrs[] = {
    &quantum_attr.attr,
    &qset_attr.attr,
    NULL,
};

int scull_dev_sysfs_init(struct scull_dev *sdev) {
    int err = 0;

    sdev->kobj = kobject_create_and_add(SCULL_NAME, NULL);
    if (!sdev->kobj) {
        LOGE("create scull dev kobject");
        return -1;
    }
    err = sysfs_create_files(sdev->kobj, scull_attrs);
    if (err) {
        LOGE("create scull dev attributes");
        return -2;
    }
    return 0;
}

int scull_dev_sysfs_deinit(struct scull_dev *sdev) {
    if (sdev->kobj) {
        sysfs_remove_files(sdev->kobj, scull_attrs);
    }
    kobject_put(sdev->kobj);

    return 0;
}